"""
@Time ： 2024/5/10 11:34
@Auth ： czm
@File ：db_util.py
@IDE ：PyCharm
"""
import pymysql

dbinfo = {
    "host": "172.19.242.85",
    "user": "wxhj",
    "password": "Wxhj123456",
    "port": 3306
}


class DbConnect():
    def __init__(self, db_cof, database="enterprise"):
        self.db_cof = db_cof
        # 连接数据库
        self.db = pymysql.connect(
            database=database,
            cursorclass=pymysql.cursors.DictCursor,
            **db_cof
        )
        # 使用cursor()方法获取操作游标，创建游标
        self.cursor = self.db.cursor()

    def select(self, sql):
        # sql查询
        self.cursor.execute(sql)
        results = self.cursor.fetchall()
        return results

    def excute(self, sql):
        # sql 删除、提交、修改语句
        try:
            # 执行sql语句
            self.cursor.execute(sql)
            # 提交修改
            self.db.commit()
        except:
            # 发生错误时回滚
            self.db.rollback()

    def close(self):
        # 关闭数据库连接
        self.db.close()


if __name__ == '__main__':
    db = DbConnect(dbinfo)
    sql1 = "select * from tenant_department where tenant_id='1773512906344808450';"
    result = db.select(sql1)
    print(result)
